package org.easyspring.util;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class PointCityDataUtils {//NOSONAR

    public static final String STR = "·";
    //港澳台
    private static List<Integer> specialRegions = Arrays.asList(1003254,1003274,1003229);
    //直辖市 北京1000003 天津1000021 上海1000795 重庆1002245
    private static List<Integer> municipalityRegions = Arrays.asList(1000003,1000021,1000795,1002245);
    private static List<String> spaecialAddress = Arrays.asList("香港·中国","澳门·中国","台湾·中国");
    private static List<String> spaecialAddress2 = Arrays.asList("中国香港·中国","中国澳门·中国","中国台湾·中国");
    private  static String taiwanCitys="1003230,1003231,1003232,1003233,1003234,1003235,1003236,1003237,1003238,1003239,1003240,1003241,1003242,1003243,1003244,1003245,1003246,1003247,1003248,1003249,1003250,1003251,1003252,1003253,1081852";

    public static String transPointCityData(ContentVo vo, List<RelationData> regions,Map<Long,List<ContentPoiRelationVo>> poiRelationVoMap) {//NOSONAR
        List<Integer> taiwanCityList = transList(taiwanCitys);
        if(vo.getContentType().equals(1)){
            //动态
            //港澳poi打点统一处理
            if(null != vo.getCityId() && !poiRelationVoMap.isEmpty() && !poiRelationVoMap.get(vo.getContentId()).isEmpty()){
                //港澳poi打点
                if(specialRegions.contains(vo.getCityId()) || taiwanCityList.contains(vo.getCityId())){
                    List<RelationData> cityEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getCityId())).collect(Collectors.toList());
                    List<ContentPoiRelationVo> contentPoiRelationVos = poiRelationVoMap.get(vo.getContentId());
                    return contentPoiRelationVos.get(0).getTargetName() + STR + cityEntitys.get(0).getTargetName();
                }
            }
            if(null ==vo.getCityId() && null== vo.getProvinceId() && null !=vo.getCountryId()){
                //1.打点国家，直接返回国家名称
                List<RelationData> countryEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getCountryId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(countryEntitys)){
                    return countryEntitys.get(0).getTargetName();
                }
            }else if(null ==vo.getCityId() && null!= vo.getProvinceId()){
                //2.打点省份，直接返回省份
                List<RelationData> provinceEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getProvinceId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(provinceEntitys)){
                    return provinceEntitys.get(0).getTargetName();
                }
            }else if(null !=vo.getCityId() && (municipalityRegions.contains(vo.getCityId()) || specialRegions.contains(vo.getCityId()))){
                //打点为城市，港澳和直辖市直接返回城市名称
                List<RelationData> cityEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getCityId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(cityEntitys) && (poiRelationVoMap.isEmpty() || null ==poiRelationVoMap.get(vo.getContentId()))){
                    return cityEntitys.get(0).getTargetName();
                }
            }else if(null !=vo.getCityId() && !specialRegions.contains(vo.getCityId()) && !municipalityRegions.contains(vo.getCityId())
                    && !taiwanCityList.contains(vo.getCityId()) //NOSONAR
                    && null !=vo.getProvinceId()
                    && null !=vo.getCountryId() && vo.getCountryId().equals(1000001)){
                 //city_id不为空,查看country_id=1000001,改成城市+省份[只有普通城市需要处理,特殊城市港澳台、直辖市不用处理要排除掉]
                List<RelationData> provinceEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getProvinceId())).collect(Collectors.toList());
                List<RelationData> cityEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getCityId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(provinceEntitys)  && CollectionUtils.isNotEmpty(cityEntitys)){
                    return cityEntitys.get(0).getTargetName()+STR+provinceEntitys.get(0).getTargetName();
                }
            }else if(null !=vo.getCityId() && null !=vo.getCountryId() && !vo.getCountryId().equals(1000001)){
            //city_id不为空,查看country_id!=1000001,不是打点的poi,改成城市+国家
                List<RelationData> countryEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getCountryId())).collect(Collectors.toList());
                List<RelationData> cityEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getCityId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(countryEntitys) && CollectionUtils.isNotEmpty(cityEntitys) && (poiRelationVoMap.isEmpty() || null ==poiRelationVoMap.get(vo.getContentId()))){
                    return cityEntitys.get(0).getTargetName()+STR+countryEntitys.get(0).getTargetName();
                }
            }
        }else{
            //文章
            if(spaecialAddress.contains(vo.getAddress()) || spaecialAddress2.contains(vo.getAddress())){
                //address为香港·中国/澳门·中国/台湾·中国不动
                return vo.getAddress();
            }else if(null !=vo.getCityId() && municipalityRegions.contains(vo.getCityId()) && StringUtils.isNotEmpty(vo.getAddress())){
                //city_id不为空,查看city_id是不是直辖市的，是就改为城市
                return vo.getAddress().substring(0,vo.getAddress().indexOf(STR));
            }else if(null !=vo.getCityId() && null !=vo.getCountryId() && vo.getCountryId().equals(1000001) && !specialRegions.contains(vo.getCityId())){
               //country_id为中国,city_id不为空,改为城市+省份 港澳排除掉
                List<RelationData> provinceEntitys = regions.stream().filter(p -> p.getTargetId().equals(vo.getProvinceId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(provinceEntitys) && StringUtils.isNotEmpty(vo.getAddress())){
                    return vo.getAddress().substring(0,vo.getAddress().indexOf(STR))+STR+provinceEntitys.get(0).getTargetName();
                }
            }else if(null== vo.getCityId() && null !=vo.getProvinceId() && null !=vo.getCountryId() && vo.getCountryId().equals(1000001) && StringUtils.isNotEmpty(vo.getAddress())){
              //country_id为中国,city_id为空,province_id不为空,改为省份
                if(vo.getAddress().contains(STR) ){return  vo.getAddress().substring(0,vo.getAddress().indexOf(STR));}//NOSONAR
            }
        }

        if(null !=vo.getCityId() && null !=taiwanCityList && taiwanCityList.contains(vo.getCityId()) && StringUtils.isNotEmpty(vo.getAddress())
                && (poiRelationVoMap.isEmpty() || null ==poiRelationVoMap.get(vo.getContentId()))){
            //台湾下的城市,除了poi打点外，改为 xxx·中国台湾
            return vo.getAddress().contains(STR) ? vo.getAddress().substring(0,vo.getAddress().indexOf(STR))+"·中国台湾" : vo.getAddress()+"·中国台湾";
        }else{
            //其他就直接取address
            if(StringUtils.isNotEmpty(vo.getAddress())){
                if(vo.getAddress().contains("·香港")){
                    return replaceTargetName(vo.getAddress(),"香港");
                }else if(vo.getAddress().contains("·澳门")){
                    return replaceTargetName(vo.getAddress(),"澳门");
                }else if(vo.getAddress().contains("·台湾")){
                    return replaceTargetName(vo.getAddress(),"台湾");
                }

            }
            return vo.getAddress();
        }
    }

    public static String replaceTargetName(String input, String replaceData){
        // 使用正则表达式匹配 ".香港" 只在它位于字符串末尾或后面跟非单词字符时进行替换
        String regex = "(?<=\\w)\\·"+replaceData+"(?=\\W|$)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        // 替换为 ".中国香港"
        return matcher.replaceAll("·中国"+replaceData);
    }
    
    public static List<Integer> transList(String str){
        if(StringUtils.isNotEmpty(str)){
            return Arrays.asList(str.split(",")).stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());//NOSONAR
        }
        return null;//NOSONAR
    }
}
